package net.realsee.common.enums.wcs.plc;

import cn.hutool.core.util.StrUtil;
import net.realsee.common.utils.spring.SpringUtils;
import net.realsee.framework.plc.IPlcReadWrite;

/**
 * Plc通讯方式枚举
 *
 * @author 孙正华
 * @since 2023/8/15 13:52
 */
public enum PlcCommTypeEnum {

    MODBUS("Modbus", "Modbus通讯协议", "modbusReadWriteImpl"),
    S7_PROTOCOL("S7Protocol", "S7协议", "s7ProtocolReadWriteImpl"),
    ;

    private final String code;
    private final String message;
    private final String handleService;

    PlcCommTypeEnum(String code, String message, String handleService) {
        this.code = code;
        this.message = message;
        this.handleService = handleService;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getHandleService() {
        return handleService;
    }

    /**
     * 通过code匹配对应的处理类
     */
    public static IPlcReadWrite getHandleService(String code) {
        if (StrUtil.isBlank(code)) {
            return null;
        }
        for (PlcCommTypeEnum item : PlcCommTypeEnum.values()) {
            if (StrUtil.equalsIgnoreCase(item.code, code)) {
                return SpringUtils.getBean(item.getHandleService());
            }
        }
        return null;
    }
}
